<template>
  <div>
    <el-button
      style="float: left; margin-left: 15px"
      icon="el-icon-plus"
      type="primary"
      plain
      size="small"
      @click="show"
      >新增</el-button
    ><el-button
      style="float: left; margin-left: 15px"
      type="danger"
      plain
      icon="el-icon-delete"
      size="small"
      @click="delAll"
      :disabled="this.multipleSelection.length == 0"
      >批量删除</el-button
    >
    <el-input
      style="width: 200px; float: left; margin-left: 20px"
      size="small"
      placeholder="请输入公告序号"
      v-model="id"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 20px"
      size="small"
      placeholder="请输入公告标题"
      v-model="title"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 20px"
      size="small"
      placeholder="请输入创建人员"
      v-model="createBy"
    />
    <el-select
      v-model="type"
      style="width: 200px; float: left; margin-left: 20px"
      size="small"
      clearable
      placeholder="请选择公告类型"
    >
      <el-option
        v-for="item in options"
        :key="item"
        :label="item"
        :value="item"
      >
      </el-option>
    </el-select>
    <el-button
      type="primary"
      icon="el-icon-search"
      style="float: left; margin-left: 20px"
      size="small"
      @click="initNotice"
      >搜索</el-button
    >
    <el-button
      style="float: left; margin-left: 15px; margin-bottom: 12px"
      icon="el-icon-refresh"
      size="small"
      @click="clearInput"
      >重置</el-button
    >
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%; margin-top: 10px"
      :cell-style="rowClass"
      :header-cell-style="headClass"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45"> </el-table-column>
      <el-table-column prop="id" label="序号" width="100"></el-table-column>
      <el-table-column
        prop="title"
        label="公告标题"
        width="400"
      ></el-table-column>
      <el-table-column prop="type" label="公告类型" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == '公告'" type="success">{{
            scope.row.type
          }}</el-tag>
          <el-tag v-else type="warning">通知</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人员" width="150">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="200">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 1.5%; float: right">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="currentPage"
        :page-sizes="[5, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="添加公告"
      :visible.sync="dialogVisible"
      width="400px"
      center
    >
      <el-form
        :model="formData"
        ref="registerForm"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item prop="title" label="标题">
          <el-input
            v-model="formData.title"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item prop="type" label="类型">
          <el-select v-model="formData.type" clearable placeholder="请选择类型">
            <el-option
              v-for="item in options"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="content" label="内容">
          <el-input
            type="textarea"
            v-model="formData.content"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="small" @click="dialogVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="addNotice"
          >确定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="编辑公告"
      :visible.sync="editDialogVisible"
      width="400px"
      center
    >
      <el-form
        :model="editData"
        ref="editForm"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item prop="title" label="标题">
          <el-input
            v-model="editData.title"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item prop="type" label="类型">
          <el-select v-model="editData.type" clearable placeholder="请选择类型">
            <el-option
              v-for="item in options"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="content" label="内容">
          <el-input
            type="textarea"
            v-model="editData.content"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="editDialogVisible = false"
          >取消</el-button
        >
        <el-button size="mini" type="primary" @click="updateNotice"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      editDialogVisible: false,
      tableData: [],
      currentPage: 1,
      pageSize: 8,
      total: 0,
      id: "",
      title: "",
      type: "",
      createBy: "",
      options: ["通知", "公告"],
      multipleSelection: [],
      formData: {
        title: "",
        content: "",
        type: "",
      },
      editData: {
        id: "",
        title: "",
        content: "",
        type: "",
        createBy: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 3,
            max: 250,
            message: "长度在 3 到 250 个字符",
            trigger: "blur",
          },
        ],
        type: [{ required: true, message: "请选择标题", trigger: "change" }],
      },
    };
  },
  created() {
    this.initNotice();
  },
  methods: {
    show() {
      this.formData = {};
      this.dialogVisible = true;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initNotice();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.initNotice();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    rowClass() {
      return "text-align: center;";
    },
    headClass() {
      return "text-align: center;background:#f8f8f9;";
    },
    clearInput() {
      location.reload();
    },
    handleEdit(row) {
      this.editData.id = row.id;
      this.editData.title = row.title;
      this.editData.type = row.type;
      this.editData.content = row.content;
      this.editData.createBy = JSON.parse(
        sessionStorage.getItem("user")
      ).username;
      this.editDialogVisible = true;
    },
    updateNotice() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          if (this.editData.type == "公告") {
            if (
              this.editData.content == null ||
              this.editData.content.trim().length <= 0
            ) {
              this.$message.error("公告内容不能为空");
            } else {
              this.$post("notice/", this.editData).then((res) => {
                if (res.code == 200) {
                  this.initNotice();
                  this.editDialogVisible = false;
                }
              });
            }
          } else {
            this.$post("notice/", this.editData).then((res) => {
              if (res.code == 200) {
                this.initNotice();
                this.editDialogVisible = false;
              }
            });
          }
        } else {
          this.$message.error("数据校验失败，请检查");
        }
      });
    },
    handleDelete(row) {
      this.$confirm(
        '此操作将删除公告序号为"' + row.id + '"的数据, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$delete("notice/?ids=" + row.id).then((res) => {
          if (res.code == 200) {
            this.initNotice();
          }
        });
      });
    },
    addNotice() {
      this.$refs["registerForm"].validate((valid) => {
        if (valid) {
          let param = {
            title: this.formData.title,
            content: this.formData.content,
            type: this.formData.type,
            createBy: JSON.parse(sessionStorage.getItem("user")).username,
          };
          if (this.formData.type == "公告") {
            if (
              this.formData.content == null ||
              this.formData.content.trim().length <= 0
            ) {
              this.$message.error("公告内容不能为空");
            } else {
              this.$post("notice/", param).then((res) => {
                if (res.code == 200) {
                  this.initNotice();
                  this.dialogVisible = false;
                }
              });
            }
          } else {
            this.$post("notice/", param).then((res) => {
              if (res.code == 200) {
                this.initNotice();
                this.dialogVisible = false;
              }
            });
          }
        } else {
          this.$message.error("数据校验失败，请检查");
        }
      });
    },
    initNotice() {
      this.$get(
        "notice/" +
          this.currentPage +
          "/" +
          this.pageSize +
          "?id=" +
          this.id +
          "&title=" +
          this.title +
          "&createBy=" +
          this.createBy +
          "&type=" +
          this.type
      ).then((res) => {
        if (res.code == 200 && res.code) {
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      });
    },
    delAll() {
      this.$confirm(
        '此操作将删除"' + this.multipleSelection.length + '"个职位, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let ids = "?";
          this.multipleSelection.forEach((item) => {
            ids += "ids=" + item.id + "&";
          });
          this.$delete("notice/" + ids).then((res) => {
            if (res.code == 200) {
              this.initNotice();
            }
          });
        })
        .catch(() => {
          this.initNotice();
        });
    },
  },
};
</script>

<style>
</style>